package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.vo.TeacherQueue;
import com.atguigu.eduservice.service.EduTeacherService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.lang.model.element.VariableElement;
import javax.websocket.server.PathParam;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-10-21
 */
@RestController
@ResponseBody
@RequestMapping("/eduservice/teacher")
@CrossOrigin
public class EduTeacherController {

    //查询讲师表所有数据
    //注入service
    @Autowired
    private EduTeacherService teacherService;

    @GetMapping("findAll")
    public R  findAllTeacher(){
        List<EduTeacher> list = teacherService.list(null);
        return R.ok().data("items", list);
    }
    @DeleteMapping( "{id}")
    public R removeTeacher(@PathVariable String id){
        boolean flag = teacherService.removeById(id);
        if(flag){
            return  R.ok();
        }else{
            return R.error();
        }

    }

    @GetMapping("pageTeacher/{current}/{limit}")
    public  R pageListTeacher(@PathVariable long current,
                              @PathVariable long limit ){
        Page<EduTeacher> pageTeacher=new Page(1,3);
        teacherService.page(pageTeacher,null);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();

        return R.ok().data("total",total).data("rows",records);
    }

    @PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current,
                                  @PathVariable long limit ,
                                  @RequestBody(required=false)   TeacherQueue teacherQueue){
        Page<EduTeacher> pageTeacher=new Page<>(current,limit);
        //创建page对象

        //构建条件
        QueryWrapper<EduTeacher> wrapper=new QueryWrapper<>();
        String begin = teacherQueue.getBegin();
        String end = teacherQueue.getEnd();
        Integer level = teacherQueue.getLevel();
        String name = teacherQueue.getName();
        //判断是否为空
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if(!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if(!StringUtils.isEmpty(end)){
            wrapper.le("gmt_modified",end);
        }
        wrapper.orderByDesc("gmt_create");
        //调用方法进行查询
        teacherService.page(pageTeacher,wrapper);
        long total = pageTeacher.getTotal();
        List<EduTeacher> records = pageTeacher.getRecords();

        return R.ok().data("total",total).data("rows",records);

    }
    @PostMapping("addTeacher")
    public R add(@RequestBody EduTeacher eduTeacher){
        boolean save = teacherService.save(eduTeacher);
        if(save){
            return R.ok();
        }else{
            return R.error();
        }
    }
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        EduTeacher eduTeacher = teacherService.getById(id);
        return R.ok().data("teacher",eduTeacher);
    }

    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody EduTeacher eduTeacher){
        boolean flag = teacherService.updateById(eduTeacher);
    if(flag){
        return R.ok();
    }
    else {
        return R.error();
    }
    }
}

